// index.js
const app = getApp()
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';
// 引入用户模型
const user = require('../../utils/user')
// 引入 


Page({
	mixins: [require('../../mixins/tabbar.js')],
	/**
	 * 页面的初始数据
	 */
	data: {
		isAdmin: false,
		page: 0,
		limit: 10,
		leftImageList: [],
		leftBlockHeight: 0,
		rightImageList: [],
		rightBlockHeight: 0,
		isLogin: false,
		tabbarCur: 0
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		// 获取登录状态
		wx.getSystemInfo({
			success: (result) => {
				this.setData({
					imgWidth: (result.screenWidth - 20) / 2 - 10,
				})
			},
		})
		this.getImageList()
	},
	/**
	 * 获取照片列表数据
	 */
	getImageList: function () {
		let {
			page,
			limit,
			leftImageList,
			rightImageList,
			leftBlockHeight,
			rightBlockHeight
		} = this.data;
		let that = this;
		wx.cloud.database()
			.collection("Posts")
			.skip(page * limit)
			.limit(limit)
			.get()
			.then((res) => {
				if (res.data.length > 0) {
					// console.log(res)
					// 循环
					// let _itemHeight = parseInt(Math.round(allData[i].img_height * that.data.imgW / allData[i].img_width));

					res.data.forEach((item, index) => {
						// console.log(item, index)
						let _imgHeight = parseInt(Math.round(res.data[index].images[0].height * 345 / res.data[index].images[0].width));
						// console.log(_imgHeight)
						// if (leftHeight == rightHeight || leftHeight < rightHeight) {
						if (leftBlockHeight == rightBlockHeight || leftBlockHeight < rightBlockHeight) {
							leftImageList.push(res.data[index])
							leftBlockHeight += _imgHeight
						} else {
							rightImageList.push(res.data[index])
							rightBlockHeight += _imgHeight
						}
					})
					that.setData({
						page: page + 1,
						leftBlockHeight,
						rightBlockHeight,
						leftImageList,
						rightImageList
					})
				} else {
					console.log("没有了")
					Notify({
						type: 'danger',
						message: '到底了 ~'
					});
				}
			})
	},

	/**
	 * 瀑布流展示图片
	 * @param {} e 
	 */

	

	imgDetail: function (e) {
		// console.log(e.currentTarget.dataset.id)
		wx.navigateTo({
			url: '../images/detail/detail?id=' + e.currentTarget.dataset.id,
		})
	},

	// onTabbarChange: function (e) {
	// 	console.log(e)
	// 	this.toNav(e)
	// },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		console.log("pages/index/index:onShow")
		// 加载会员信息
		user.login()
		.then((ret) => {
			// console.log(ret)
			app.globalData.isLogin = true
			app.globalData.user = ret
			this.setData({
				user: ret,
				isLogin: true
			})
		})
		.catch(() => {
			// 用户未注册, 进行注册提示
			console.log("未注册")
		})
		// let adminList = app.globalData.admin;
		// if (adminList.indexOf(app.globalData.userOpenId) != '-1') {
		// 	this.setData({
		// 		isAdmin: true
		// 	})
		// }
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		this.getImageList()
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})